﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFramework.ORM;
using MiniWeb.Core.Entities;
using WFramework.MvcPager;
using MiniWeb.Core.Dtos;
using WFramework;

namespace MiniWeb.Dao
{
    public class LinksDao : BaseDao<Links>
    {
        public PagedList<Links> GetPageList(int? id, string linkName,  int pageIndex, int pageSize)
	    {
		    var sql = Sql.Builder.Select("*").From("Links with(nolock)").Where("1=1");
		    if(id !=null)
		    {
			    sql.Where("ID=@0",id);	
		    }

            if (!string.IsNullOrEmpty(linkName))
            {
                sql.Where("linkName LIKE @0", string.Format("%{0}%", linkName));
            }

		    sql.OrderBy("ID DESC");
            return PagedList<Links>(pageIndex, pageSize, sql);
	    }

        public List<Links> GetList() 
        {
            var sql = Sql.Builder.Select("ID,linkName,linkAddress").From("Links with(nolock)").Where("1=1");         
            return Query<Links>(sql).ToList();
        }

        public Links GetById(int id)
        {
            var sql = Sql.Builder.Where("id=@0",id);
            return FirstOrDefault(sql);
        }
        public Links GetByName(string linkName)
        {
            var sql = Sql.Builder.Where("linkName=@0", linkName);
            return FirstOrDefault(sql);
        }

        public void Delete(int id)
        {
            var sql = Sql.Builder.Where("id=@0", id);
            Delete(sql);
        }

    }
}
